package cn.wolfcode.wolf2w.note.controller;

import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import cn.wolfcode.wolf2w.common.security.utils.SecurityUtils;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import cn.wolfcode.wolf2w.note.api.domain.Note;
import cn.wolfcode.wolf2w.note.api.domain.NoteComment;
import cn.wolfcode.wolf2w.note.query.NoteCommentQuery;
import cn.wolfcode.wolf2w.note.service.INoteCommentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 游记评论 Controller
 * 
 * @author dalang
 * @date 2023-11-27
 */
@RestController
@RequestMapping("noteComments")
public class NoteCommentController {
    @Autowired
    private INoteCommentService noteCommentService;

    /**
     * 游记评论详情
     */
    @GetMapping("/detail/{id}")
    public R<NoteComment> detail(@PathVariable Long id) {
        NoteComment noteComment = noteCommentService.getById(id);
        return R.ok(noteComment);
    }

    /**
     * 游记评论 列表
     */
    @GetMapping("/query")
    public R<IPage<NoteComment>> query(NoteCommentQuery qo) {
        IPage<NoteComment> page = noteCommentService.queryPage(qo);

        return R.ok(page);
    }

    @RequiresLogin
    @PostMapping("/add")
    public R<?> add(@RequestBody NoteComment comment) {
        comment.setUserId(SecurityUtils.getUserId());
        comment.setCreateTime(new Date());

        if (comment.getRefId() == null) {
            comment.setType(NoteComment.TYPE_COMMENT);
        }else {
            comment.setType(NoteComment.TYPE_COMMENT_TO_COMMENT);
        }
        noteCommentService.save(comment);
        return R.ok();
    }

    /**
     * 获取最新10条评论
     */
    @GetMapping("/newCommontsTop10")
    public R<List<NoteComment>> getNewCommontsTop10() {
        List<NoteComment> list = noteCommentService.newCommontsTop10();

        return R.ok(list);
    }


    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<NoteComment>> feignList() {
        return R.ok(noteCommentService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<NoteComment> feignGet(@PathVariable Long id) {
        return R.ok(noteCommentService.getById(id));
    }
}
